package com.zhiwang.planet.widget

import android.animation.ValueAnimator
import android.content.Context
import android.util.AttributeSet
import android.util.Log
import android.view.View
import android.view.ViewGroup
import androidx.core.view.NestedScrollingParent2
import androidx.core.view.ViewCompat

class OverScrollLayout(context: Context?, attrs: AttributeSet?) : ViewGroup(context, attrs), NestedScrollingParent2 {

    val TAG = "OverScrollLayout"

    init {

    }


    var axes: Int = ViewCompat.SCROLL_AXIS_VERTICAL


    override fun addView(child: View?) {
        super.addView(child)
        if (childCount > 1)
            throw  IllegalStateException("can only host one child view!!!") as Throwable
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        measureChild(getChildAt(0), widthMeasureSpec, heightMeasureSpec)
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {

        val view = getChildAt(0)
        view.layout(0, 0, view.measuredWidth, view.measuredHeight)
    }


    override fun onNestedPreScroll(target: View, dx: Int, dy: Int, consumed: IntArray, type: Int) {

    }

    override fun onStopNestedScroll(target: View, type: Int) {

        settle(target)
    }

    override fun onStartNestedScroll(child: View, target: View, axes: Int, type: Int) = true

    override fun onNestedScrollAccepted(child: View, target: View, axes: Int, type: Int) {
        this.axes = axes
    }

    override fun onNestedScroll(target: View, dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int, type: Int) {

        Log.i(TAG, "onNestedScroll: $dxConsumed   $dxUnconsumed  $dyConsumed   $dyUnconsumed")
        if (axes == ViewCompat.SCROLL_AXIS_VERTICAL)
            handleVerticalOverScroll(dyUnconsumed)
        else if (axes == ViewCompat.SCROLL_AXIS_HORIZONTAL)
            handleHorizontalOverScroll(dxUnconsumed)

    }

    private fun handleHorizontalOverScroll(dxUnconsumed: Int) {

        val view = getChildAt(0)

        view.layout(view.left + dxUnconsumed, view.top, view.left + dxUnconsumed + view.width, view.bottom)
    }

    private fun handleVerticalOverScroll(dyUnconsumed: Int) {
        val view = getChildAt(0)

        view.layout(view.left, view.top - dyUnconsumed, view.right, view.bottom - dyUnconsumed)
    }


    private fun settle(target: View) {

        val factor0 = 0

        val factor1 = 1

        val factor2 = 0

        val factor3 = 1


        val startOffset = if (axes == ViewCompat.SCROLL_AXIS_VERTICAL) -target.top else target.left

        val end = 0

        ValueAnimator.ofInt(startOffset, end)
                .apply {
                    duration = 250
                    addUpdateListener {
                        val delta = it.animatedValue as Int

                        target.layout(
                                delta * factor0,
                                delta * factor1,
                                delta * factor0 + target.width,
                                delta * factor1 + target.height)
                    }
                    start()
                }
    }

}